package com.kasax.android.arch.base.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Message;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.kasax.android.arch.handler.WeakHandler;

public abstract class BaseActivity extends AppCompatActivity {
    protected Context mCtx;

    @SuppressLint("HandlerLeak")
    private WeakHandler weakHandler = new WeakHandler(this) {

        @Override
        protected void onMessageHandle(Message msg) {
            processHandlerMessage(msg);
        }
    };

    protected void processHandlerMessage(Message msg) {

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.mCtx = this;
        injectBeforeSetContentView();
        if (needAutoSetContentView() && getContentLayoutId() != 0) {
            setContentView(getContentLayoutId());
        }
        injectBeforeInitView();
        initView();
        addListener();
        initData();
    }

    protected boolean needAutoSetContentView() {
        return true;
    }

    @Override
    protected void onDestroy() {
        weakHandler.reset();
        super.onDestroy();
    }

    protected WeakHandler getWeakHandler() {
        return weakHandler;
    }

    protected abstract int getContentLayoutId();

    protected void initView() {
    }

    protected void addListener() {
    }

    protected void initData() {
    }

    /**
     * 在initView之前添加操作
     */
    protected void injectBeforeInitView() {

    }

    /**
     * 在setContentView之前添加的操作
     */
    protected void injectBeforeSetContentView() {

    }
}
